﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace LightControls
{
	public partial class SliderItem : UserControl, ISliderItem
	{
		public override String Text { get; set; }
		public String Source { get; set; }
		public String Username { get; set; }
		public Image Avatar { get; set; }
		public String TimeStamp { get;set;}
		public String Link { get; set;}
		public Object Value {get; set;}
		public Boolean isSelected { get; set;}

		private String strFooter;

		public SliderItem(String username, String source, String text, String timestamp, Object value)
		{
			InitializeComponent();
			this.Height = 100;
			Text = text;
			Source = source;
			Username = username;
			Value = value;
			TimeStamp = timestamp;

			if ( Text.Length > 140 )
			{
				Text = Text.Substring(0,140) + "...";
			}
			strFooter = String.Format("{0}, {1} via {2}", Username, TimeStamp, Source);
		}

		public SliderItem(String username, String source, String text, String timestamp, String link, Object value)
		{
			InitializeComponent();
			this.Height = 100;
			Text = text;
			Source = source;
			Username = username;
			Value = value;
			Link = link;
			TimeStamp = timestamp;

			if ( Text.Length > 140 )
			{
				Text = Text.Substring(0,140) + "...";
			}
			strFooter = String.Format("{0}, {1} via {2}", Username, TimeStamp, Source);
		}

		public SliderItem(String username, String source, String text, String timestamp, Image avatar, Object value)
		{
			InitializeComponent();
			this.Height = 100;
			this.Width = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Width;
			Text = text;
			Source = source;
			Username = username;
			Value = value;
			TimeStamp = timestamp;
			Avatar = avatar;
			if ( Text.Length > 140 )
			{
				Text = Text.Substring(0,140) + "...";
			}
			strFooter = String.Format("{0}, {1} via {2}", Username, TimeStamp, Source);
		}

		protected override void OnResize( EventArgs e )
		{
			this.Width = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Width;
			this.Refresh();
			base.OnResize( e );
		}

		protected override void OnPaintBackground( PaintEventArgs e )
		{
			//Do nothing here
		}

		protected override void OnPaint( PaintEventArgs e )
		{
			
			using ( var currentRender = new Bitmap( this.Width, this.Height ) )
			using ( var currentRenderGraphics = Graphics.FromImage( currentRender ) )
			{
				if ( !isSelected )
				{
					currentRenderGraphics.FillRectangle( new SolidBrush( Color.Black ), 0, 0, this.Width, this.Height );
				}
				else
				{
                    Gradient.GradientFill.Fill(currentRenderGraphics, new Rectangle(0, 0, this.Width, this.Height), Color.Black, Color.Orange, Gradient.GradientFill.FillDirection.TopToBottom);
					//currentRenderGraphics.FillRectangle(  SolidBrush( Colo ), 0, 0, this.Width, this.Height );
				}
                Font fontText = new Font(FontFamily.GenericSansSerif, 10f, FontStyle.Regular);
                Font fontFooter = new Font(FontFamily.GenericSansSerif, 7.5f, FontStyle.Regular);


                SizeF sizeFooter = currentRenderGraphics.MeasureString(strFooter, fontFooter);
				SizeF strUsernameSize = currentRenderGraphics.MeasureString(Username, fontText);
				SizeF strTextSize = currentRenderGraphics.MeasureString(Text, fontText);
				if ( Avatar != null )
				{
					Rectangle rectAvatar = new Rectangle( 2, 2, 60, 70 );
					currentRenderGraphics.DrawImage(Avatar,rectAvatar,new Rectangle(0,0,Avatar.Width,Avatar.Height), GraphicsUnit.Pixel);
				}
                Rectangle rectFooter = new Rectangle( 2, this.Height - (int)sizeFooter.Height, (int)sizeFooter.Width, (int)sizeFooter.Height);
                Rectangle rectText = new Rectangle(65, 2, this.Width - 75, this.Height - (int)sizeFooter.Height);
				
				currentRenderGraphics.DrawString(Text, fontText, new SolidBrush(Color.White), rectText);
                currentRenderGraphics.DrawString(strFooter, fontFooter, new SolidBrush(Color.White), rectFooter);
				
				currentRenderGraphics.DrawLine(new Pen(Color.Orange),this.Location.X, 0, this.Location.X + this.Width, 0);
				//currentRenderGraphics.FillEllipse(new SolidBrush(Color.White), 0,82,4,4);
				e.Graphics.DrawImage(currentRender, 0, 0);
			}
		}
	}
}
